import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/common/setting_util.dart';
import 'package:package_info/package_info.dart';

class AboutPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => AboutPageState();
}

class AboutPageState extends State<AboutPage> {
  String _appName = "", _version = "", _packageName = "", _buildNumber = "";

  @override
  Widget build(BuildContext context) {
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      if (!mounted) return;
      setState(() {
        _appName = packageInfo.appName;
        _packageName = packageInfo.packageName;
        _version = packageInfo.version;
        _buildNumber = packageInfo.buildNumber;
      });
    });

    String _versionInfo = "";
    if (Platform.isIOS) {
      _versionInfo = "For iOS v";
    } else if (Platform.isAndroid) {
      _versionInfo = "For Android v";
    }

    return Scaffold(
      appBar: _createAppBar(),
      body: ListView(
        children: <Widget>[
          Container(
            alignment: Alignment.center,
            margin: const EdgeInsets.only(top: 20.0, bottom: 20.0),
            child: Text(
              _appName,
              style: TextStyle(
                  fontSize: 28.0,
                  color: ColorCommon.black1,
                  fontWeight: FontWeight.bold),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              _versionInfo + _version + " build" + _buildNumber,
              style: TextStyle(
                fontSize: 16.0,
                color: ColorCommon.grey3,
              ),
            ),
          ),
          Container(
            alignment: Alignment.center,
            margin: const EdgeInsets.only(top: 20.0, bottom: 20.0),
            child: new CachedNetworkImage(
              width: 125.0,
              height: 125.0,
              imageUrl:
                  Platform.isIOS ? SettingUtil.iQRUrl : SettingUtil.aQRUrl,
              fit: BoxFit.fill,
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              "扫描二维码, 您的朋友也可以下载 " + _appName + " 客户端!",
              style: TextStyle(
                fontSize: 16.0,
                color: ColorCommon.grey3,
              ),
            ),
          ),
        ],
      ),
    );
  }

  _createAppBar() {
    return AppBar(
      elevation: 0.0,
      title: Text("关于"),
      leading: IconButton(
        icon: Icon(Icons.arrow_back_ios),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
    );
  }
}
